package com.zhangtai.utils.Excel.office;

import cn.hutool.core.date.DateUtil;
import org.apache.commons.beanutils.Converter;
import org.apache.commons.lang.math.NumberUtils;

import java.util.Date;

public class DateConvert implements Converter {

	@Override
	public Object convert(Class type, Object value) {
		if (value == null) {
			return null;
		}

		if (value instanceof Date) {
			return value;
		}

		if (value instanceof Long) {
			Long longValue = (Long) value;
			return new Date(longValue.longValue());
		}

		try {
			return DateUtil.parseDate(value.toString());
		} catch (Exception e) {
			try {
				return DateUtil.parseTime(value.toString());
			} catch (Exception e1) {
				if (value instanceof String) {
					String str = (String) value;
					return new Date(NumberUtils.toLong(str));
				}
			}
		}
		return null;
	}

}
